//dommRoom/domRoomType -> <... roomId=""><... roomtypeid="">
//dom -> <... resId="" from="" to="">
//obj.customize.roomTypeId
//obj.customize.roomId
//obj.customize.from
//obj.customize.to
//obj.customize.resId
function fillRoomDropdown(domRoom,domRoomType,dom,callback,command,customize)
{
	var obj = {};
	obj.dom = dom;
	obj.domRoom = domRoom;
	obj.domRoomType = domRoomType;
	obj.selectRoom = $("<select name=\"splitToRoom\" style=\"width:100%\"></select>");
	obj.selectRoomType = $("<select name=\"splitToRoom\" style=\"width:100%\"></select>");
	obj.callback = callback;
	obj.command = command;
	obj.customize = customize;
	
	domRoom.css("display","none");
	domRoomType.css("display","none");
	
	domRoom.after(obj.selectRoom);
	domRoomType.after(obj.selectRoomType);

	//fill roomtype
	for(var i=0;i<SharedData.RoomType.length;i++)
		obj.selectRoomType.append(optionString.format(SharedData.RoomType[i].id,SharedData.RoomType[i].shortName));
	if(obj.customize == null)
		obj.selectRoomType.val(domRoomType.attr("roomtypeid"));
	else
		obj.selectRoomType.val(obj.customize.roomTypeId);
	
	obj.selectRoomType.change(function() { DropDownChangeRoomType(obj); });
	obj.selectRoom.change(function() { selectRoomDropdown(obj); });
	DropDownChangeRoomType(obj);
}
function DropDownChangeRoomType(obj)
{
	var roomtype = obj.selectRoomType.find(":selected").val();
	var from = (obj.customize == null)? new Date(obj.dom.attr("from")) : new Date(obj.customize.from);
	var to = (obj.customize == null)? new Date(obj.dom.attr("to")) : new Date(obj.customize.to);
	var span = daysBetween(from,to);

	var except = (obj.customize == null)? obj.dom.attr("resId") : obj.customize.resId;
	loadRoomOnPopupWithException(roomtype,from.toFormatString('yyyy-MM-dd'),span,except,loadRoomDropdown,obj);
	obj.selectRoom.focus();
	$(document).mousedown(function(e) {
		obj.newTarget = $(e.target);
    });
	obj.selectRoom.blur(function(e) {
		if(obj.newTarget.get(0) != obj.selectRoomType.get(0) && obj.newTarget.get(0) != obj.selectRoom.get(0)){
			if(obj.selectRoom.find(":selected").val() != "-")selectRoomDropdown(obj);
			finishChangeRoomDropDown(obj);
		}
		obj.Target = null;
	});

	obj.selectRoomType.blur(function(e) {
		if(obj.newTarget.get(0) != obj.selectRoomType.get(0) && obj.newTarget.get(0) != obj.selectRoom.get(0)){
			if(obj.selectRoom.find(":selected").val() != "-")selectRoomDropdown(obj);
			finishChangeRoomDropDown(obj);
		}
		obj.Target = null;
	});
}
function loadRoomDropdown(data,obj){
	var objArray = JSON.parse(data);

	obj.selectRoom.find("option").remove();
	for(i=0;i<objArray.length;i++)
	{
		obj.selectRoom.append(optionString.format(objArray[i].id,objArray[i].roomName));
	}
	var roomId = (obj.customize == null)? obj.domRoom.attr("roomid") : obj.customize.roomId;
	if(obj.selectRoom.find("[value=" + roomId + "]").length == 0)
		obj.selectRoom.prepend(optionString.format("-",simpleLabel_Select));
	if(objArray.length != 0)
	{
		if(obj.selectRoom.find("option[value="+roomId+"]").length == 0)
			obj.selectRoom.val("-");
		else
			obj.selectRoom.val(roomId);
	}
}
function finishChangeRoomDropDown(obj){
	if(obj == null) return;
	if(obj.selectRoom != null) obj.selectRoom.remove();
	if(obj.selectRoomType != null) obj.selectRoomType.remove();
	if(obj.domRoom != null) obj.domRoom.css("display","");
	if(obj.domRoomType != null) obj.domRoomType.css("display","");
}
function selectRoomDropdown(obj){
	var id = (obj.customize == null)? obj.dom.attr("resId") : obj.customize.resId;
	var room = obj.selectRoom.val();
	var roomstring = obj.selectRoom.find(":selected").text();
	var check = (obj.customize == null)? obj.dom.text() == roomstring : obj.customize.roomId == obj.selectRoom.find(":selected").val();
	if(check)
		finishChangeRoomDropDown(obj);
	else
		if(obj.command == null)
			moveReservationToSpecificRoomWithAssign(id,room,obj.callback,obj);
		else
			obj.command(obj);
}


//dom -> <... refid="">
//callback ->  callback(obj)
function fillSimpleDropdown(dom,data,datavalue,datatext,callback,defaultShowSelect,style) {var obj={};obj.dom=dom;obj.data=data;obj.select=(style==null||style=="")?$("<select style=\"width:100%\"></select>"):$("<select style=\""+style+"\"></select>");obj.callback=callback;dom.css("display","none");dom.after(obj.select);for(var i=0;i<data.length;i++)obj.select.append(optionString.format(data[i][datavalue],data[i][datatext]));if((defaultShowSelect!=null&&defaultShowSelect)||obj.select.find("[value="+dom.attr("refid")+"]").length==0)obj.select.prepend(optionString.format("-",simpleLabel_Select));obj.select.val(dom.attr("refid"));obj.select.focus();obj.select.change(function(){if((defaultShowSelect!=null&&defaultShowSelect)||obj.select.find(":selected").val()!="-")callback(obj);finishChangeSimpleDropdown(obj)});obj.select.blur(function(e){finishChangeSimpleDropdown(obj)})}
function fillSimpleDropdownWithAttr(dom,data,datavalue,datatext,dataattr,callback,defaultShowSelect,style) {var obj={};obj.dom=dom;obj.data=data;obj.select=(style==null||style=="")?$("<select style=\"width:100%\"></select>"):$("<select style=\""+style+"\"></select>");obj.callback=callback;dom.css("display","none");dom.after(obj.select);for(var i=0;i<data.length;i++)obj.select.append(optionStringWithAttr.format(data[i][datavalue],data[i][datatext],data[i][dataattr]));if((defaultShowSelect!=null&&defaultShowSelect)||obj.select.find("[value="+dom.attr("refid")+"]").length==0)obj.select.prepend(optionStringWithAttr.format("-",simpleLabel_Select,"-"));obj.select.val(dom.attr("refid"));obj.select.focus();obj.select.change(function(){if((defaultShowSelect!=null&&defaultShowSelect)||obj.select.find(":selected").val()!="-")callback(obj);finishChangeSimpleDropdown(obj)});obj.select.blur(function(e){finishChangeSimpleDropdown(obj)})}
function fillSimpleNumberDropdown(dom,numfrom,numto,callback,defaultShowSelect,style) {var obj={};obj.dom=dom;obj.numfrom=numfrom;obj.numto=numto;obj.select=(style==null||style=="")?$("<select style=\"width:100%\"></select>"):$("<select style=\""+style+"\"></select>");obj.callback=callback;dom.css("display","none");dom.after(obj.select);for(var i=numfrom;i<=numto;i++)obj.select.append(optionString.format(i,i));if((defaultShowSelect!=null&&defaultShowSelect)||obj.select.find("[value="+dom.attr("refid")+"]").length==0)obj.select.prepend(optionString.format("-",simpleLabel_Select));obj.select.val(dom.attr("refid"));obj.select.focus();obj.select.change(function(){if((defaultShowSelect!=null&&defaultShowSelect)||obj.select.find(":selected").val()!="-")callback(obj);finishChangeSimpleDropdown(obj)});obj.select.blur(function(e){finishChangeSimpleDropdown(obj)})}
function finishChangeSimpleDropdown(obj) {if(obj==null)return;if(obj.select!=null)obj.select.remove();obj.dom.css("display","")}

function fillSimpleTextEdit(e,t,n,r,i){if(!e.is(":visible"))return;var s={};s.dom=e;var r=r==null||r==""?"width:100%;":r;r=t?r+"text-align:right;":r;s.textbox=$('<input type="text" style="'+r+'"></input>');if(i!=null&&i)ForceNumericOnly(s.textbox);s.callback=n;e.css("display","none");var o=t?currencyStringToNumber(e.html()):e.html();e.after(s.textbox);s.textbox.val(o);s.textbox.focus();s.textbox.keydown(function(e){if(e.keyCode==13&&!e.ctrlKey){n(s);finishChangeSimpleTextEdit(s)}});$(document).mousedown(function(e){s.newTarget=$(e.target)});s.textbox.blur(function(e){if(!$(s.newTarget).is(s.textbox)){n(s);finishChangeSimpleTextEdit(s)}s.newTarget=null})}
function fillSimpleTextEditWithNumpad(e,t,n,r,i){if(!e.is(":visible"))return;var s={};s.dom=e;var r=r==null||r==""?"width:100%;":r;r=t?r+"text-align:right;":r;s.textbox=$('<input type="text" style="'+r+'"></input>');if(i!=null&&i)ForceNumericOnly(s.textbox);s.callback=n;e.css("display","none");var o=t?currencyStringToNumber(e.html()):e.html();e.after(s.textbox);s.textbox.val(o);s.textbox.focus();s.textbox.keydown(function(e){if(e.keyCode==13&&!e.ctrlKey){$("#BlankDialog table.numpad").parents("#BlankDialog:visible").dialog("destroy");n(s);finishChangeSimpleTextEdit(s)}});numpad(s.textbox[0],null,function(e,t,r){n(r);finishChangeSimpleTextEdit(r)},s)}
function finishChangeSimpleTextEdit(obj) {if(obj==null)return;if(obj.textbox!=null)obj.textbox.remove();obj.dom.css("display","")}
function fillDropdown(dom,data,textValue,textHtml){
	dom.find("option").not(":first").remove();
	if(data == null) return;
	dom.prepend(optionString.format("-",simpleLabel_Select));
	for(i=0;i<data.length;i++)
		dom.append("<option value='" + data[i][textValue]+ "'>" + data[i][textHtml] + "</option>");

}